package cn.marinda.util;

import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;

import java.text.DecimalFormat;
import java.util.HashSet;
import java.util.Set;

/**
 * 服务器工具类
 *
 * @author admin
 * @date 2022/07/08
 */
public class ServerUtils {
    public static long[] prevTicks;
    public static long[] ticks;
    public static long nice,irq,softirq,steal,csys,user,iowait,idle,totalCpu,totalByte,acaliableByte;
    public static SystemInfo systemInfo;
    public static CentralProcessor processor;
    public static GlobalMemory memory;
    public static boolean openServerFlag = false;
    public static boolean validFileInfoFlag = false;
    public static boolean writeServerPropertiesFlag = false;

//    用来区分创建服务端核心下载操作和在线核心下载操作的flag true => 创建服务端， false => 在线核心下载
    public static boolean downloadCoreFlag = false;

    /**
     * 服务器相关控制
     */

    public static Set<String> serverPluginSet = new HashSet<>();
    public static String serverCore;

    public static void printAll(){
        System.out.println("-------------------Info-------------------");
        System.out.println(getCPUCount());
        System.out.println(getCPUName());
        System.out.println(getCPURate());
        System.out.println(getRamCount());
        System.out.println(getRamRate());
        System.out.println(getRamTotal());
    }

    /**
     * 分割开始字符串
     *
     * @return {@link String}
     */
    public static String splitStartString(String str){
        int endIndex = str.indexOf("]");
        String newStr = str.substring(endIndex+1);
        return newStr;
    }

    /**
     * 加载默认参数
     */
    public static void loadDefaultArgs() {
        try {
            systemInfo = new SystemInfo();
            processor = systemInfo.getHardware().getProcessor();
            prevTicks = processor.getSystemCpuLoadTicks();
            // 睡眠1s
            ticks = processor.getSystemCpuLoadTicks();
            nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
            irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
            softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
            steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
            csys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
            user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
            iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
            idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
            totalCpu = user + nice + csys + idle + iowait + irq + softirq + steal;
            memory = systemInfo.getHardware().getMemory();
            //总内存
            totalByte = memory.getTotal();
            //剩余
            acaliableByte = memory.getAvailable();

        }catch (Exception e){
            e.printStackTrace();
        }
    }



    /**
     * 得到cpu 核心数量
     *
     * @return {@link String}
     */
    public static String getCPUCount() {
        return "核心数：" + processor.getLogicalProcessorCount();
    }

    public static String getCPURate(){
        return "CPU使用率：" + new DecimalFormat("#.##%").format(1.0-(idle * 1.0 / totalCpu));
    }

    /**
     * 得到cpu 名称
     *
     * @return {@link String}
     */
    public static String getCPUName(){
        return processor.getName();
    }

    /**
     * 获得内存总数
     *
     * @return {@link String}
     */
    public static String getRamTotal(){
        return "总内存：" + formatByte(totalByte);
    }

    /**
     * 获得内存速度
     *
     * @return {@link String}
     */
    public static String getRamRate(){
        return "内存使用率：" + new DecimalFormat("#.##%").format((totalByte-acaliableByte)*1.0/totalByte);
    }

    /**
     * 使用内存数量
     *
     * @return {@link String}
     */
    public static String getRamCount(){
        return "使用内存：" + formatByte(totalByte-acaliableByte);
    }

    /**
     * 获得剩余内存数量
     *
     * @return {@link String}
     */
    public static String getSurplus(){
        return "剩余内存：" + formatByte(acaliableByte);
    }


    //二进制转换
    private static String formatByte(long byteNumber){
        //换算单位
        double FORMAT = 1024.0;
        double kbNumber = byteNumber/FORMAT;
        if(kbNumber<FORMAT){
            return new DecimalFormat("#.##KB").format(kbNumber);
        }
        double mbNumber = kbNumber/FORMAT;
        if(mbNumber<FORMAT){
            return new DecimalFormat("#.##MB").format(mbNumber);
        }
        double gbNumber = mbNumber/FORMAT;
        if(gbNumber<FORMAT){
            return new DecimalFormat("#.##GB").format(gbNumber);
        }
        double tbNumber = gbNumber/FORMAT;
        return new DecimalFormat("#.##TB").format(tbNumber);
    }

}
